package de.iritgo.skillfull.component;

import java.util.List;

import de.iritgo.skillfull.component.visitor.Visitor;
import de.iritgo.skillfull.entity.Entity;
import de.iritgo.skillfull.entity.EntityImpl;

public abstract class ComponentImpl implements Component
{
	/* The comp id */
	protected String id;
	
	/* The comp name */
	protected String name;
	
	/* The description */
	protected String description;
	
	/* The entity */
	protected Entity entity;

	/* The active flag */
	protected boolean active;

	/* Child component list */
	protected List<Component> components;
	
	/**
	 * Set the child components with spring
	 * 
	 * @param components The components
	 */
	public void setComponents (List<Component> components)
	{
		this.components = components;
	}

	/**
	 * @see de.iritgo.skillfull.component.Component#getComponents()
	 */
	public List<Component> getComponents ()
	{
		return components;
	}
	
	public String getId ()
	{
		return id;
	}

	public void setId (String id)
	{
		this.id = id;
	}

	public String getName ()
	{
		return name;
	}

	public void setName (String name)
	{
		this.name = name;
	}

	public void setDescription (String description)
	{
		this.description = description;
	}

	public String getDescription ()
	{
		return description;
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#init()
	 */
	public void init ()
	{
		if (components == null)
			return;
			
		for (Component component : components)
		{
			component.init ();
		}		
	}

	/**
	 * @see de.iritgo.skillfull.component.Component#setActive(boolean)
	 */
	public void setActive (boolean active)
	{
		this.active = active;
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#removed(de.iritgo.skillfull.entity.Entity)
	 */
	public void removed(Entity entity) 
	{
		if (components == null)
				return;
		
		for (Component component : components)
		{
			component.removed (entity);
		}
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#hasComponent(java.lang.String)
	 */
	public boolean hasComponent (String id)
	{
		if (id == null || components == null)
			return false;
		
		for (Component component : components)
		{
			if (component.getId ().equals (id))
				return true;
		}
		return false;
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#getComponentById(java.lang.String)
	 */
	public Component getComponentById (String id)
	{
		if (id == null || components == null)
			return null;
		
		for (Component component : components)
		{
			if (component.getId ().equals (id))
				return component;
		}
		return null;
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#setEntity(de.iritgo.skillfull.entity.Entity)
	 */
	public void setEntity (Entity entity) 
	{
		this.entity = entity;
		
		if (components == null)
			return;
		
		for (Component component : components)
		{
			component.setEntity(entity);
		}		
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#deactivate()
	 */
	public void deactivate () 
	{
		active = false;
		
		if (components == null)
			return;
			
		for (Component component : components)
		{
			component.deactivate ();
		}		
	}
	
	/**
	 * @see de.iritgo.skillfull.component.Component#getEntity()
	 */
	public Entity getEntity ()
	{
		return entity;
	}

	/**
	 * @see de.iritgo.skillfull.component.Component#printConfiguration()
	 */
	abstract public void printConfiguration ();
	
	/**
	 * @see de.iritgo.skillfull.component.Component#accept(de.iritgo.skillfull.component.visitor.Visitor)
	 */
	abstract public void accept (Visitor visitor);
}
